﻿using System;
using System.Collections.Generic;
using System.Text;
using dnAnalytics.LinearAlgebra;
using VectorMath;

namespace CellularAutomata {
    public class Automata {
        int gridHeight;
        int gridWidth;
        int gridDepth;
        double gravityForce;
        double thermalDiffusion;
        double factor;
        double beta;

        private Boolean[, ,] liquidSwap;
        private Boolean[, ,] solidSwap;
        private Double[, ,] deltaAmount;
        private Double[, ,] deltaEnergy;

        private Boolean[, ,] solid;
        public Boolean[, ,] Solid {
            get { return solid; }
            set { solid = value; }
        }

        private Boolean[, ,] liquid;
        public Boolean[, ,] Liquid {
            get { return liquid; }
            set { liquid = value; }
        }

        private Double[, ,] amount;
        public Double[, ,] Amount {
            get { return amount; }
            set { amount = value; }
        }

        private Double[, ,] energy;
        public Double[, ,] Energy {
            get { return energy; }
            set { energy = value; }
        }

        public Automata(int x, int y, int z) {
            gridHeight = y;
            gridWidth = x;
            gridDepth = z;

            liquidSwap = new Boolean[x, y, z];
            solidSwap = new Boolean[x, y, z];

            solid = new Boolean[x, y, z];
            liquid = new Boolean[x, y, z];

            deltaAmount = new Double[x, y, z];
            deltaEnergy = new Double[x, y, z];
            amount = new Double[x, y, z];
            energy = new Double[x, y, z];
        }

        public void Gravity(double dt) {
            
            for (int x = 0; x < gridWidth; x++) {
                for (int y = 0; y < gridDepth; y++) {
                    for (int z = 0; z < gridHeight-1; z++) {
                        liquidSwap[x, y, z + 1] = liquid[x, y, z] && !solid[x, y, z + 1];
                        if (liquidSwap[x, y + 1, z]) {
                            deltaAmount[x, y, z + 1] += amount[x, y, z] * factor;
                            deltaAmount[x, y, z] -= amount[x, y, z] * factor;
                            deltaEnergy[x, y, z + 1] += energy[x, y, z] * beta * factor;
                            deltaEnergy[x, y, z] -= energy[x, y, z] * beta * factor;
                        }
                    }
                }
            }
        }

        public void Spreading(double dt) {

            for (int x = 0; x < gridWidth; x++) {
                for (int y = 0; y < gridWidth; y++) {
                    for (int z = 0; z < gridHeight; z++) {
                        if (x > 0) {

                        }
                        if (x < gridWidth - 1) {

                        }
                   
                    }
                }
            }
        }

        public void Step(double dt) {


            Gravity(dt);


        }


    }
}
